﻿Public Class Representantes
    Private _indic As UInt16 = 1
    Private _nuevo As Boolean = False
    Private _callFromForm As Boolean = False
    Private cambios, notDataSaved As Boolean
    Private EnlaceTabla As New BindingSource
    Private totalCampos As UInt16 = 0

    WriteOnly Property callFromForm() As Boolean
        Set(ByVal value As Boolean)
            _callFromForm = value
        End Set
    End Property

    WriteOnly Property indic() As UInt16
        Set(ByVal Value As UInt16)
            _indic = Value
        End Set
    End Property

    WriteOnly Property nuevo() As Boolean
        Set(ByVal Value As Boolean)
            _nuevo = Value
        End Set
    End Property


    Private Sub Cliente_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        cambios = True
        BtnClear.Enabled = IIf(_nuevo, True, False)
    End Sub

    Private Sub Enlaces()
        totalCampos = 0
        For Each Objeto As Control In Me.Controls
            If TypeOf Objeto Is TextBox Then
                If Not _nuevo Then Objeto.DataBindings.Add("Text", EnlaceTabla, Objeto.Tag)
                AddHandler Objeto.TextChanged, AddressOf Cliente_TextChanged
                totalCampos += 1
            End If
        Next
        If Not _nuevo Then _
            CliNum.DataBindings.Add("Text", EnlaceTabla, "idRepresentante")
    End Sub

    Private Sub insertUpdate(ByVal newClient As Boolean)
        Dim Valores(totalCampos - 1) As String
        Dim Campos(totalCampos - 1) As String
        Dim k As UInt16 = 0
        Dim procName As String = IIf(newClient, "insertRepresentante", "updateRepresentante")

        If Not newClient Then
            ReDim Valores(totalCampos)
            ReDim Campos(totalCampos)
            Campos.SetValue("@" + CliNum.Tag, k)
            Valores.SetValue(CliNum.Text, k)
            k += 1
        End If

        For Each Objeto As Control In Me.Controls
            If TypeOf Objeto Is TextBox Then
                Campos.SetValue("@" + Objeto.Tag, k)
                Valores.SetValue(Objeto.Text, k)
                k += 1
            End If
        Next
        If Not DataAccess.InsertNewRecord(Campos, Valores, procName) Then
            MessageBox.Show("Error al guardar registro de Cliente")
        Else
            If _callFromForm Then
                Dim frm
                For Each frm In My.Application.OpenForms
                    Console.WriteLine(frm.name)
                    If frm.name = "Clientes" Then frm.updateRepresentantes()
                Next
                frm = Nothing
            Else
                Dim frm
                For Each frm In My.Application.OpenForms
                    Console.WriteLine(frm.name)
                    If frm.name = "ListaClientes" Then frm.refrescarDataGrid()
                Next
                frm = Nothing
            End If
            If newClient Then
                cleanForm()
            Else
                notDataSaved = False
                Me.Close()
                Me.Dispose()
            End If
        End If
    End Sub

    Private Sub cleanForm()

        For Each Objeto As Control In Me.Controls
            Objeto.DataBindings.Clear()
            If TypeOf Objeto Is TextBox Then Objeto.Text = ""
        Next
        CliNum.DataBindings.Clear()
        CliNum.Text = ""
        Enlaces()
    End Sub

    Private Sub LblClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If cambios Then
            Dim msg = "¿Desea salir del módulo Representantes?"
            Dim title = "¡Advertencia!"
            Dim style = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or _
                        MsgBoxStyle.Critical
            notDataSaved = IIf(MsgBox(msg, style, title) = MsgBoxResult.Yes, False, True)
        End If
        Me.Close()
        If Not notDataSaved Then Me.Dispose()
    End Sub

    Private Sub Representantes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not _nuevo Then EnlaceTabla.DataSource = _
            DataAccess.GetRecords("SELECT * FROM Representantes WHERE idRepresentante=" & _indic.ToString, CommandType.Text)
        Enlaces()
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        insertUpdate(_nuevo)
    End Sub
End Class